package com.rtwo.tio.server.handler;

import com.rtwo.tio.client.msg.P2PRequest;
import com.rtwo.tio.common.BasicHandler;
import com.rtwo.tio.common.BasicPacket;
import com.rtwo.tio.common.MsgType;
import com.rtwo.tio.common.SessionContext;
import com.rtwo.tio.server.msg.P2PResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.utils.json.Json;

import java.io.UnsupportedEncodingException;

/**
 * Created by hasee on 2018/7/19.
 */
public class P2PResponseHandler extends BasicHandler<P2PRequest> {

    private Logger logger = LoggerFactory.getLogger(P2PResponseHandler.class);

    @Override
    public Class<P2PRequest> getClazz() {
        return P2PRequest.class;
    }

    @Override
    public void postHanlder(P2PRequest body, ChannelContext channelContext) throws UnsupportedEncodingException {
        logger.error("收到点对点请求消息:{}", Json.toJson(body));
        P2PResponse p2pRespBody = new P2PResponse(body.getMsg(),(String)channelContext.getAttribute("username"));
        String p2pRespBodyJsonStr = Json.toJson(p2pRespBody);
        Tio.sendToUser(channelContext.getGroupContext(), body.getToUserId(), new BasicPacket(MsgType.P2P_RESP, p2pRespBodyJsonStr.getBytes()));
    }
}
